clear
set mem 300m

capture log close
log using "$dataloc/Log/ifs_metrics", text replace
set more off

/* --------------------------------------------------------------- *
   Author: Ed Al-Hussainy
   Description:  Generate metrics using IFS data
 * --------------------------------------------------------------- */

forvalues i = 1/10 { 
    tempfile tmp`i' 
}

/* --------------------------------------------------------------- *
    IFS data + GDP + CPI
 * --------------------------------------------------------------- */

use "$dataloc/Data/IFS/$dataver/IFS", clear

merge cncode year using "$dataloc/Output/GDP" "$dataloc/Output/CPI" "$dataloc/Output/WDI_WEOmetrics"
drop _merge*

do "$codeloc/country_selector"

/* --------------------------------------------------------------- *    
   filter out garbage values      
 * --------------------------------------------------------------- */  

sum value*
foreach i of var value* {
   replace `i' = . if `i' <= 0
}

/* --------------------------------------------------------------- *    
   corrections      
 * --------------------------------------------------------------- */      
      
replace value22D = . if cncode == "MOZ" & year < 1990     
replace value22D = . if cncode == "HRV" & year == 1993
replace value22D = . if cncode == "ZAR" & year < 1987 


foreach i of var value* {
   local j = strpos("`i'", "60")
   di "`i ' | `j'"
   if `j' == 0 {
      replace `i' = . if cncode == "MOZ" & year < 1990
      replace `i' = . if cncode == "HRV" & year == 1993
      replace `i' = . if cncode == "ZAR" & year < 1987 
   }
   else {
      replace `i' = . if cncode == "NIC" & year <= 1990
   }      
}

* Ecuador GDP is in USD (xrate = 1) 
replace wdi_xrate = 1 if cncode == "ECU" 

 
/* --------------------------------------------------------------- *
   final metrics
 * --------------------------------------------------------------- */

/* Structure DB:

pcrdbgdp PRIVATE CREDIT BY DEPOSIT MONEY BANKS / GDP value22D


dbacba   DEPOSIT MONEY BANK ASSETS / (DEPOSIT MONEY + CENTRAL) BANK ASSETS
llgdp LIQUID LIABILITIES / GDP
cbagdp   CENTRAL BANK ASSETS / GDP
dbagdp   DEPOSIT MONEY BANK ASSETS / GDP
ofagdp   OTHER FINANCIAL INSTITUTIONS ASSETS / GDP

pcrdbofgdp  PRIVATE CREDIT BY DEPOSIT MONEY BANKS AND OTHER FINANCIAL INSTITUTIONS / GDP
bdgdp BANK DEPOSITS / GDP
fdgdp FINANCIAL SYSTEM DEPOSITS / GDP
bcbd  BANK CREDIT / BANK DEPOSITS
ll_usd   LIQUID LIABILITIES (IN MIL. 2000 USD)


* Bank Credit / Bank Deposits
gen bcbd = _t7/_t9
label var bcbd "Bank Credit / Bank Deposits"

* Central Bank Assets / GDP
deflator cbagdp _t1 gdp "Central Bank Assets / GDP"

* Deposit Money Bank Assets / GDP
deflator dbagdp _t2 gdp "Deposit Money Bank Assets / GDP"

* Other Financial Institutions Assets / GDP
deflator ofagdp _t3 gdp "Other Financial Institutions Assets / GDP" 

* Private Credit by Deposit Money Banks / GDP
deflator pcrdbgdp _t7 gdp "Private Credit by Deposit Money Banks / GDP"

* Private Credit by Deposit Money Banks and Other Financial Institutions / GDP
deflator pcrdbofgdp _t8 gdp "Private Credit / GDP"

* Bank Deposits / GDP
deflator bdgdp _t9 gdp "Bank Deposits / GDP"
* gen bdgdp_unadjusted = _t9/gdp

* Financial System Deposits / GDP
deflator fdgdp _t10 gdp "Financial System Deposits / GDP"

*FinStats


gen S01IFS0 =  value22D / SGDP
label var S01IFS0 "DMB CLAIMS ON THE DOMESTIC REAL PRIVATE SECTOR / GDP (IFS)"
*flagger S01IFS0 "value22D SGDP SCPI"
*series_meta S01IFS0 "22D"

egen numerator = rowtotal(value22F value22G) 
gen S02IFS0 =  numerator / SGDP
label var S02IFS0 "DMB CLAIMS ON THE DOMESTIC NON-MONETARY FINANCIAL SECTOR / GDP (IFS)"
*flagger S02IFS0 "numerator value22F value22G SGDP SCPI"
*series_meta S02IFS0 "22F 22G"
drop numerator


egen numerator = rowtotal(value22A value22B value22C)
gen S03IFS0 =  numerator / SGDP
label var S03IFS0 "DMB CLAIMS ON THE GOVERNMENT AND SOES / GDP (IFS)"
*flagger S03IFS0 "numerator value22A value22B value22C SGDP SCPI"
*series_meta S03IFS0 "22A 22B 22C"
drop numerator


* total DMB assets
*egen _t91 = rowtotal(value22A value22B value22C value22D) 
* other fin inst assets
*egen _t92 = rowtotal(value42A value42B value42C value42D value42H) 

gen S04IFS0 =   value20_ / SGDP
label var S04IFS0 "DMB CLAIMS ON THE MONETARY AUTHORITY / GDP (IFS)"
*flagger S04IFS0 "value20_ SGDP SCPI"
*series_meta S04IFS0 "20_"


egen numerator = rowtotal(value21_ value21N)
gen S05IFS0 = numerator / SGDP
label var S05IFS0 "DMB FOREIGN CLAIMS / GDP (IFS)"
*flagger S05IFS0 "numerator value21_ value21N SGDP SCPI"
*series_meta S05IFS0 "21_ 21N"
drop numerator


egen numerator = rowtotal(value24_ value25A value25B value25L value25_)
gen S06IFS0 =  numerator / SGDP
label var S06IFS0 "DMB DOMESTIC DEPOSITS / GDP (IFS)"
*flagger S06IFS0 "numerator value24_ value25A value25B value25L value25_ SGDP SCPI"
*series_meta S06IFS0 "24_ 25A 25B 25L 25_"
drop numerator


gen S07IFS0 =  value60P - value60L
label var S07IFS0 "LENDING-DEPOSIT SPREAD"
*flagger S07IFS0 "value60P value60L SCPI"
*series_meta S07IFS0 "60P 60L"


egen numerator = rowtotal(value20_ value22A value22B value22C value21_ value21N)
egen denominator = rowtotal(value20_ value22A value22B value22C value21_ value21N value22D value22F value22G)
gen S08IFS0 = numerator / denominator
label var S08IFS0 "SYSTEMICALLY LIQUID ASSETS TO TOTAL DMB CLAIMS (IFS)"
*flagger S08IFS0 "numerator denominator value20_ value22A value22B value22C value21_ value21N value22D value22F value22G SCPI"
drop numerator denominator
*series_meta S08IFS0 "20_ 22A 22B 22C 21_ 21N 22D 22F 22G"


egen denominator = rowtotal(value24_ value25A value25B value25L value25_)
gen S09IFS0 =  value22D / denominator
label var S09IFS0 "CLAIMS TO THE DOMESTIC NON-FINANCIAL PRIVATE SECTOR TO DEPOSITS (IFS)"

*/

capture program drop deflator
program deflator
    args out in1 in2 lbl
    
    sort cncode year
    
    egen _in1 = rowtotal(`in1')
    egen _in2 = rowtotal(`in2')    
    
    * simple ratio - no averaging, no CPI deflation
    by cncode: gen `out'0 = _in1 / _in2
    label var `out'0 "`lbl'"    
    
    * arth mean /w CPI deflation
    by cncode: gen `out'1 = (0.5 * ((_in1/_pe) + (_in1[_n-1]/_pe[_n-1]))) / (_in2/_p)
    label var `out'1 "`lbl' "  
    
    drop _in*
    
end 

deflator S01IFS value22D GDP "DMB CLAIMS ON PRIVATE SECTOR / GDP"

deflator S02IFS "value22A value22B value22C" GDP "DMB CLAIMS ON THE GOVERNMENT AND SOES / GDP"

deflator S03IFS "value24_ value25A value25B value25L value25_" GDP "DMB DOMESTIC DEPOSITS / GDP"

gen ll = value55L
replace ll = value35L if missing(ll) | ll == 0
label var ll "Liquid Liabilities"

deflator S04IFS ll GDP "LIQUID LIABILITIES / GDP"

gen S05IFS0 = S04IFS0 *  wdi_gdp_usd_2k
label var S05IFS0 "LIQUID LIABILITIES (MIL. 2000 USD)"

gen S05IFS1 = S04IFS1 *  wdi_gdp_usd_2k
label var S05IFS1 "LIQUID LIABILITIES (MIL. 2000 USD)"

egen cba = rowtotal (value12A value12B value12C value12D)
label var cba "Central Bank Assets"

egen dmba = rowtotal (value22A value22B value22C value22D)
label var cba "DMB Assets"

egen ofia = rowtotal (value42A value42B value42C value42D value42H)
label var ofia "Other Financial Institutions Assets"

gen S06IFS0  = dmba / (cba + dmba) 
lsabel var S06IFS0 "DMB ASSETS / DMB + CENTRAL BANK ASSETS"

deflator S07IFS dmba GDP "DMB ASSETS / GDP"

deflator S08IFS cba GDP "CENTRAL BANK ASSETS / GDP"

deflator S09IFS ofia GDP "OTHER FINANCIAL INSTITUTIONS ASSETS / GDP"

deflator S10IFS value22D GDP "DMB CLAIMS ON DOMESTIC PRIVATE SECTOR / GDP"


egen numerator = rowtotal(value24_ value25A value25B value25L value25_)


gen dmbd = 


gen S03IFS0 =  value60P - value60L
label var S03IFS0 "LENDING-DEPOSIT SPREAD"


/* --------------------------------------------------------------- *
    Save results
 * --------------------------------------------------------------- */

   

label data "IFS Metrics (Data version: $dataver)" 

keep cncode year S*
*drop if year < $startyear

save "$dataloc/Output/IFSmetrics", replace


*/
/* end of code */
clear
log close
exit

